define(function (require, exports, module) {
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	var tools = require("tools");
	
    var instance = {
        initComponent: function (params,widget) {
        	var component = new Grid({
                parentNode: ".J-grid", 
                autoRender:false,
                model : {
                    url : "api/checkincar/queryall",
                    params: function() {
                    	var subnav=widget.get("subnav");
                        return {
                        	"startDate":subnav.getValue("dateRange").start,
     						"endDate":subnav.getValue("dateRange").end,
     						"status":subnav.getValue("checkStatus"),
     						"checkInImplement.groupReserve.planItemGroup.servicePoint":subnav.getValue("servicePoint"),
     						fetchProperties: "*,checkInImplement.version," +
							"checkInImplement.pkCIImplement," +
							"checkInImplement.status," +
							"checkInImplement.carDispatchBill.*," +
							"checkInImplement.signUpNum," +
							"checkInImplement.dataSource.*," +
							"status.*," +
							"checkInImplement.groupReserve.*," +
							"checkInImplement.groupReserve.planItemGroup.allianceOrg.*," +
							"checkInImplement.groupReserve.planItemGroup.planItem.*," +
							"checkInImplement.groupReserve.planItemGroup.planItem.destination.*," +
							"checkInImplement.groupReserve.planItemGroup.servicePoint.pkServicePoint," +
							"checkInImplement.groupReserve.planItemGroup.servicePoint.name," +
							"checkInImplement.groupReserve.planItemGroup.signUps.version," +
							"checkInImplement.nonMemGroupReserve.nonMemberGroup.*," +
							"checkInImplement.nonMemGroupReserve.pkNonMemGroupReserve," +
							"checkInImplement.nonMemGroupReserve.nonMemberGroup.groupUnit," +
							"checkInImplement.nonMemGroupReserve.nonMemberGroup.servicePoint.name," +
							"checkInImplement.nonMemGroupReserve.nonMemberGroup.signUpType.*," +
							"checkInImplement.nonMemGroupReserve.nonMemberGroup.operator.servicePoint.name," +
							"checkInImplement.nonMemGroupReserve.nonMemberGroup.signUps.version" 
                        };
                    },
                    columns : [{
                    	name : "group",
                    	label : "团/散客",
                    	format : function(value, row){
                    		if(row.checkInImplement.dataSource.key == "GroupReserve"){
        						return "老人团";
        					}else{
        						return "散客";
        					}
                    	}
                    },{
                    	name:"checkInImplement.groupReserve",
        				label:"组团单位/预约人",
        				format: function(value,row){
        					if(row.checkInImplement.dataSource.key == "GroupReserve"){
        						if(value.planItemGroup.allianceOrg){
        							return value.planItemGroup.allianceOrg.name;
        						}else{
        							return value.planItemGroup.servicePoint.name
        						}
        					}else{
    							return row.checkInImplement.nonMemGroupReserve.nonMemberGroup.contactName;
        					}
        				}
        			},{
        				name:"checkInImplement.groupReserve.planItemGroup.planItem.destination.name",
        				label:"目的地",
        				format : function(value, row){
        					if(row.checkInImplement.dataSource.key == "GroupReserve"){
        						return value;
        					}else{
        						return row.checkInImplement.nonMemGroupReserve.nonMemberGroup.servicePoint.name;
        					}
        				}
					},{
						name:"checkInImplement.groupReserve.checkInDate",
						label:"入住日期",
						format: function(value, row){
							if(row.checkInImplement.dataSource.key == "GroupReserve"){
        						return moment(value).format("YYYY-MM-DD");
        					}else{
        						return moment(row.checkInImplement.nonMemGroupReserve.nonMemberGroup.checkInDate).format("YYYY-MM-DD");
        					}
						}
					},{						
						name:"checkInImplement.groupReserve.checkOutDate",
        				label:"退住日期",
        				format:function(value,row){
        					if(row.checkInImplement.dataSource.key == "GroupReserve"){
        						return moment(value).format("YYYY-MM-DD");
        					}else{
        						return moment(row.checkInImplement.nonMemGroupReserve.nonMemberGroup.checkOutDate).format("YYYY-MM-DD");
        					}
        				}
					},{
        				name:"checkInImplement.signUpNum",
        				label:"报名人数",
        			},{
        				name:"carDate",
        				label:"派车时间",
        				format:"date"
        			},{
        				name:"address",
        				label:"派车地址"
        			},{
						name:"status.value",
						label:"状态"
					},{
						name:"operate",
						label:"操作",
						format:"button",
						formatparams:[{
							key:"edit",
							text:"落实",
							show:function(value,data){
								if (data.status.key == "Pending") {
									return true;
			                    } else {
			                        return false;
			                    }
							},
							handler:function(index,data,rowEle){
								if(data.checkInImplement.status.key=="Edited" ||data.checkInImplement.status.key=="Initial"){
        							Dialog.alert({
					    				content:"此数据未准备，不得落实！"
					            	});
        						}else{
        							var form = widget.get("form");
    								var dispatchDate = moment(data.carDate).format("YYYY-MM-DD HH:mm");
    								widget.show([".J-form"]).hide([".J-grid"]);
    								widget.get("subnav").show(["back"]).hide(["search","checkStatus","servicePoint","dateRange"]);
    								form.reset();
    								form.setValue("version", data.version);
    								form.setValue("pkCheckInCar", data.pkCheckInCar);
//    								form.setValue("haveCar", data.haveCar);
    								form.setValue("address", data.address);
    								form.setValue("dispatchDate", dispatchDate);
    								form.setValue("description", data.description);
    								form.setValue("checkInImplement", data.checkInImplement.pkCIImplement);
//    								form.setReadonly("haveCar",true);
//    								form.setReadonly("carDate",true);
    								form.hide(["companyName","carModel","driverName"]);
    								if(data.checkInImplement.groupReserve==null){
    									form.setValue("nonMemGroupReserve",data.checkInImplement.nonMemGroupReserve.pkNonMemGroupReserve);
    									form.setValue("groupReserveName",data.checkInImplement.nonMemGroupReserve.nonMemberGroup.contactName);
    									form.setValue("destination",data.checkInImplement.nonMemGroupReserve.nonMemberGroup.servicePoint.name);
    									form.setValue("checkInDate",moment(data.checkInImplement.nonMemGroupReserve.nonMemberGroup.checkInDate).format("YYYY-MM-DD"));
    									form.setValue("checkOutDate",moment(data.checkInImplement.nonMemGroupReserve.nonMemberGroup.checkOutDate).format("YYYY-MM-DD"));
    									form.setValue("signUpNum",data.checkInImplement.signUpNum);
    								}else{
    									form.setValue("groupReserve",data.checkInImplement.groupReserve.pkGroupReserve);
    									form.setValue("groupReserveName",data.checkInImplement.groupReserve.planItemGroup.planItem.destination.name);
    									form.setValue("destination",data.checkInImplement.groupReserve.planItemGroup.servicePoint.name);
    									form.setValue("checkInDate",moment(data.checkInImplement.groupReserve.checkInDate).format("YYYY-MM-DD"));
    									form.setValue("checkOutDate",moment(data.checkInImplement.groupReserve.checkOutDate).format("YYYY-MM-DD"));
    									form.setValue("signUpNum",data.checkInImplement.signUpNum);
    								}
        						}
							}	
						}]
					}]
                }
            });
            widget.set("grid",component);
        }
    };
    module.exports = instance;
});